package com.zt.ftphelper.view;

import com.zt.ftphelper.Config;
import com.zt.ftphelper.model.ServerItem;
import com.zt.ftphelper.util.SimpleAlert;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zt
 * @Description:
 * @date 27/08/2019 13:10
 */
public class ServerEditDialogController {


    private SettingsController settingsController;

    @FXML
    private TextField serverField;

    @FXML
    private TextField userNameField;

    @FXML
    private TextField passwordField;

    @FXML
    private TextArea deployPath;

    private Stage dialogStage;
    private boolean okClicked = false;
    private ServerItem serverItem;

    @FXML
    private void initialize(){

    }


    public ServerItem getServerItem() {
        return serverItem;
    }

    public void setServerItem(ServerItem serverItem) {
        if (serverItem == null) {
            serverItem = new ServerItem();
            Integer count = Integer.valueOf(Config.getInstance().getString("ftp.server.count"));
            serverItem.setId(count + 1);
        }
        this.serverItem = serverItem;
        if (serverItem.getIp() == null) {
            serverField.setText(":");
        }else {
            serverField.setText(serverItem.getIp() + ":" + serverItem.getPort());
        }
        userNameField.setText(serverItem.getUserName());
        passwordField.setText(serverItem.getPassword());
        deployPath.setText(serverItem.getDeployPath());
    }

    public boolean isOkClicked() {
        return okClicked;
    }

    @FXML
    private void handleOk() {
        if (isInputValid()) {
            okClicked = true;
            String server = serverField.getText();
            serverItem.setIp(server.split(":")[0]);
            serverItem.setPort(server.split(":")[1]);
            serverItem.setUserName(userNameField.getText());
            serverItem.setPassword(passwordField.getText());
            serverItem.setDeployPath(deployPath.getText());

            ObservableList<ServerItem> serverData = settingsController.getServerData();
            for (int i = 0; i < serverData.size(); i++) {
                if (serverData.get(i).getId().equals(serverItem.getId())) {
                    serverData.remove(i);
                }
            }
            serverData.add(serverItem);
            dialogStage.close();
        }
    }

    @FXML
    private void handleCancel() {
        dialogStage.close();
    }

    private static final Pattern pattern = Pattern.compile("^(?:(?:2[0-4]\\d|25[0-5]|1\\d{2}|[1-9]?\\d)\\.){3}(?:2[0-4]\\d|25[0-5]|1\\d{2}|[1-9]?\\d)(?:\\:(?:\\d|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]))?$");
    private boolean isInputValid(){
        String server = serverField.getText();
        Matcher matcher = pattern.matcher(server);
        if(!matcher.matches()){
            serverField.selectAll();
            serverField.requestFocus();
            new SimpleAlert("请按照IP:端口的格式输入");
            return false;
        }
        return true;
    }

    public Stage getDialogStage() {
        return dialogStage;
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    public void setSettingsController(SettingsController settingsController) {
        this.settingsController = settingsController;
    }
}
